package com.example.shopsystem.aop;


import com.example.shopsystem.control.Result;
import com.example.shopsystem.dao.AdminMapper;
import com.example.shopsystem.dao.BusinessMapper;
import com.example.shopsystem.dao.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;

/**
 * @author : [17151]
 * @version : [v1.0]
 * @description : [过滤类]
 * @createTime : [2022/4/5 14:26]
 * @updateUser : [17151]
 * @updateTime : [2022/4/5 14:26]
 * @updateRemark : [说明本次修改内容]
 */
@WebFilter(urlPatterns = {"/filter/*"})
public class ShopFilter implements Filter {
    @Autowired
    UserMapper userMapper;
    @Autowired
    BusinessMapper businessMapper;
    @Autowired
    AdminMapper adminMapper;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("这里是过滤器！！！！");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String token;
        //强转类型
        HttpServletRequest req = (HttpServletRequest) request;
        ;
        //获取token
        token = req.getHeader("Token");
        HttpServletResponse res = (HttpServletResponse) response;
        //解析token
        if (token != "" && token != null) {
            String[] reToken = TokenMethod.decomToken(token);
            String type = reToken[3];
//            for (String A :
//                    reToken) {
//                System.out.println(A);
//            }
            if (type.equals("user")) {
                if (userMapper.queryUser(reToken[0]).getState() != 1) {
                    System.out.println("user权限错误");
                    WebUtil.writeToResp(Result.PROHIBIT, res);

                    return;
                }
            } else if (type.equals("bus")) {
                if (businessMapper.queryBusiness(reToken[0]).getState() != 1) {
                    System.out.println("bus权限错误");
                    WebUtil.writeToResp(Result.PROHIBIT, res);

                    return;

                }
            } else if (type.equals("admin")) {
                if (adminMapper.queryAdmin(reToken[0]).getState() != 1) {
                    System.out.println("admin权限错误");
                    WebUtil.writeToResp(Result.PROHIBIT, res);

                    return;

                }
            }
            //传回属性进行验证
            RequestContextHolder.currentRequestAttributes().setAttribute("id", reToken[0], SCOPE_REQUEST);
            //验证token
//            System.out.println(reToken.toString());
            String newToken = TokenMethod.isToken(reToken);
//            System.out.println(newToken+"new");
            if (!newToken.equals("")) {
                //把新的token传回头档
                res.setHeader("Token", newToken);
                System.out.println("newtoken");
            } else {
                WebUtil.writeToResp(Result.FAIL, res);
                return;
            }
            //最后都要记得指向下面
            filterChain.doFilter(request, response);
        } else {
            WebUtil.writeToResp(Result.FAIL, res);
        }
    }
}
